﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LightSwitchApplication.Web
{
    public partial class GetAppointment1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string movementId = string.Empty, movementFrom = string.Empty, movementTo = string.Empty, movementDescription = string.Empty
                    , carName = string.Empty, placeName = string.Empty, placeAddress = string.Empty;
                if (Request.Form.HasKeys())
                {
                    movementId = Request.Form["id"];
                    movementFrom = DateTime.Parse(Request.Form["from"]).ToString("yyyyMMddThhmm00");
                    movementTo = DateTime.Parse(Request.Form["to"]).ToString("yyyyMMddThhmm00");
                    movementDescription = Request.Form["desc"];
                    carName = Request.Form["car"];
                    placeName = Request.Form["place"];
                    placeAddress = Request.Form["address"];
                }
                else if (Request.QueryString.HasKeys())
                {
                    movementId = Request.QueryString["id"];
                    movementFrom = DateTime.Parse(Request.QueryString["from"]).ToString("yyyyMMddThhmm00");
                    movementTo = DateTime.Parse(Request.QueryString["to"]).ToString("yyyyMMddThhmm00");
                    movementDescription = Request.QueryString["desc"];
                    carName = Request.QueryString["car"];
                    placeName = Request.QueryString["place"];
                    placeAddress = Request.QueryString["address"];
                }
                //var requestStream = Request.InputStream;

                // Return the file
                Response.ClearHeaders();
                Response.AddHeader("content-disposition", string.Format("attachment; filename=movement_{0}.ics", movementId));

                Response.ClearContent();
                Response.ContentEncoding = System.Text.Encoding.Unicode;
                Response.ContentType = "text/plain";

                //FileStream sourceFile = new FileStream(strPath, FileMode.Open);
                //long FileSize;
                //FileSize = sourceFile.Length;
                //byte[] getContent = new byte[(int)FileSize];
                //sourceFile.Read(getContent, 0, (int)sourceFile.Length);
                //sourceFile.Close();

                //Response.BinaryWrite(getContent);
                StringWriter wr = new StringWriter();
                {
                    #region build text
                    wr.WriteLine("BEGIN:VCALENDAR");
                    wr.WriteLine("PRODID:-//Microsoft Corporation//Outlook 14.0 MIMEDIR//EN");
                    wr.WriteLine("VERSION:2.0");
                    wr.WriteLine("METHOD:REQUEST");
                    wr.WriteLine("X-MS-OLK-FORCEINSPECTOROPEN:TRUE");
                    wr.WriteLine("BEGIN:VTIMEZONE");
                    wr.WriteLine("TZID:Australia/Adelaide"); //this is a key moment for your customers in your location! 
                    wr.WriteLine("BEGIN:STANDARD");
                    wr.WriteLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4");
                    //wr.WriteLine("TZOFFSETFROM:+0200");
                    //wr.WriteLine("TZOFFSETTO:+0200");
                    wr.WriteLine("END:STANDARD");
                    //wr.WriteLine("BEGIN:DAYLIGHT");
                    //wr.WriteLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=10");
                    ////wr.WriteLine("TZOFFSETFROM:+0930");
                    ////wr.WriteLine("TZOFFSETTO:+1030");
                    //wr.WriteLine("END:DAYLIGHT");
                    wr.WriteLine("END:VTIMEZONE");
                    wr.WriteLine("BEGIN:VEVENT");
                    //using (var serverContext = LightSwitchApplication.Application.CreateContext())
                    //{
                    //    string attendeeEmail = serverApplication.User.Email;
                    //    if (!string.IsNullOrEmpty(attendeeEmail))
                    //    {
                    //        wr.WriteLine("ATTENDEE;CN=" + attendeeEmail);
                    //        if (!string.IsNullOrEmpty(ccEmail))
                    //            wr.Write(";RSVP=TRUE:" + ccEmail);
                    //    }
                    //}
                    wr.WriteLine("CLASS:PUBLIC");
                    wr.WriteLine("CREATED:" + DateTime.Now.AddHours(9.30).ToString("yyyyMMddThhmm00Z")); //time offset for servers in your time zone???
                    wr.WriteLine("DTSTART:" + movementFrom);
                    if (!string.IsNullOrEmpty(movementTo))
                        wr.WriteLine("DTEND:" + movementTo);
                    wr.WriteLine("DTSTAMP:" + DateTime.Now.ToString("yyyyMMddThhmm00"));
                    wr.WriteLine("DESCRIPTION:" + movementDescription);
                    wr.WriteLine("LAST-MODIFIED:20120321T112208Z");

                    if (!string.IsNullOrEmpty(placeAddress))
                        wr.WriteLine("LOCATION;LANGUAGE=en:" + placeAddress);

                    //wr.WriteLine("ORGANIZER;LANGUAGE=en;CN=\"" + carName + "\":mailto:" + ccEmail);
                    wr.WriteLine("PRIORITY:5");
                    wr.WriteLine("SEQUENCE:0");
                    wr.WriteLine("SUMMARY;LANGUAGE=en:Move my car " + carName + " at " + placeName);
                    wr.WriteLine("TRANSP:OPAQUE");
                    wr.WriteLine("UID:" + Guid.NewGuid().ToString().Replace("-", ""));
                    wr.WriteLine("X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//E");
                    wr.WriteLine("N\">\\n<HTML>\\n<HEAD>\\n<META NAME=\"Generator\" CONTENT=\"MS Exchange Server version 14.02.5004.000\">\\n<TITLE></TITLE>\\n</HEAD>\\n<BODY>\\n<!-- Converted from text/rtf format -->\\n\\n<P DIR=LTR><SPAN LANG=\"en\"></SPAN><SPAN LANG=\"en\"><FONT FACE=\"Calibri\">"
                        + movementDescription + "</FONT></SPAN><SPAN LANG=\"en\"></SPAN><SPAN LANG=\"en\"></SPAN></P>\\n\\n</BODY>\\n</HTML>");
                    wr.WriteLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY");
                    wr.WriteLine("X-MICROSOFT-CDO-IMPORTANCE:1");
                    wr.WriteLine("X-MICROSOFT-DISALLOW-COUNTER:FALSE");
                    wr.WriteLine("X-MS-OLK-AUTOFILLLOCATION:FALSE");
                    wr.WriteLine("X-MS-OLK-AUTOSTARTCHECK:FALSE");
                    wr.WriteLine("X-MS-OLK-CONFTYPE:0");
                    wr.WriteLine("END:VEVENT");
                    wr.WriteLine("END:VCALENDAR");
                    #endregion
                }
                wr.Close();
                Response.Write(wr);
                Response.Flush();
                Response.Close();

            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }

        }
    }
}